﻿using DLMemberPointMS.Common;
using DLMemberPointMS.Entity.POCOModel;
using DLMemberPointMS.Entity.ViewModel;
using System;
using System.Data.SqlClient;
namespace DLMemberPointMS.Business
{
    public class LConsumeOrdersService : BaseService<ConsumeOrders>
    {
        BaseService<MemCards> memCard = new BaseService<MemCards>();
        BaseService<ExchangGifts> Gifts = new BaseService<ExchangGifts>();
        //积分兑现事件
        //利于存储过程的写法
        public OperateResult InsertConsume(string MC_ID, string id, string CO_GavePoint)
        {
            SqlParameter[] para =
            {
                new SqlParameter( "@MCID",MC_ID),
                new SqlParameter("@CARDID",id),
                new SqlParameter("@Point",CO_GavePoint),
                new SqlParameter("@resault",System.Data.SqlDbType.NVarChar,20)
            };
            para[3].Direction = System.Data.ParameterDirection.Output;
            try
            {
                db.Database.ExecuteSqlCommand("exec proc_mem_consumeOrder @MCID,@CARDID,@Point,@resault output", para);
                if (!string.IsNullOrWhiteSpace(para[3].Value.ToString()))
                {
                    return new OperateResult(code.Success, "返现成功");

                }
            }
            catch(Exception e)
            {
                return new OperateResult(code.Error, e.ToString());
            }
            return new OperateResult(code.Error, "返现失败");

        }
        public OperateResult InsertExgift(string MC_ID, string id, int EG_ID, string CO_GavePoint)
        {
            SqlParameter[] para =
            {
                new SqlParameter( "@MCID",MC_ID),
                new SqlParameter("@CARDID",id),
                new SqlParameter("@EGID",EG_ID),
                new SqlParameter("@Point",CO_GavePoint),
                new SqlParameter("@resault",System.Data.SqlDbType.NVarChar,20)
            };
            para[4].Direction = System.Data.ParameterDirection.Output;
            try
            {
                db.Database.ExecuteSqlCommand("exec proc_mem_consumeOrder_Gift @MCID,@CARDID,@EGID,@Point,@resault output", para);
                if (!string.IsNullOrWhiteSpace(para[4].Value.ToString()))
                {
                    return new OperateResult(code.Success, "兑换成功！");

                }
            }
            catch (Exception e)
            {
                return new OperateResult(code.Error, e.ToString());
            }
            return new OperateResult(code.Error, "兑换失败");

        }

        //bug的写法
        //public OperateResult InsertConsume(string MC_ID, string id, string CO_GavePoint)
        //{
        //    ConsumeOrders consmerOrder = new ConsumeOrders();
        //    consmerOrder.S_ID = 2;
        //    consmerOrder.U_ID = 1;
        //    consmerOrder.CO_OrderCode = DateTime.Now.ToString() + "1";
        //    consmerOrder.CO_OrderType = 2;
        //    consmerOrder.MC_ID = Convert.ToInt32(MC_ID);
        //    consmerOrder.MC_CardID = id;
        //    consmerOrder.EG_ID = 0;
        //    consmerOrder.CO_TotalMoney = 0;
        //    consmerOrder.CO_DiscountMoney = 0;
        //    consmerOrder.CO_GavePoint = Convert.ToInt32(CO_GavePoint);
        //    consmerOrder.CO_Recash = (Convert.ToInt32(CO_GavePoint) / 10);
        //    consmerOrder.CO_Remark = "积分返现";
        //    consmerOrder.CO_CreateTime = DateTime.Now;
        //    if (Add(consmerOrder))
        //    {
        //        MemCards mem;
        //        if (id.Length < 11)
        //        {
        //            mem = memCard.Find(m => m.MC_CardID == id);
        //        }
        //        else
        //        {
        //            mem = memCard.Find(m => m.MC_Mobile == id);
        //        }
        //        if (mem != null)
        //        {
        //            mem.MC_Point = mem.MC_Point - Convert.ToInt32(CO_GavePoint);
        //            mem.MC_Money = mem.MC_Money + (Convert.ToInt32(CO_GavePoint) / 10);

        //            if (memCard.Update(mem))
        //            {
        //                return new OperateResult(code.Success, "返现成功！");
        //            }
        //        }

        //    }

        //    return new OperateResult(code.Error, "插入失败");
        //}
        //public OperateResult InsertExgift(string MC_ID, string id,int EG_ID , string CO_GavePoint)
        //{
        //    ConsumeOrders consmerOrder = new ConsumeOrders();
        //    consmerOrder.S_ID = 2;
        //    consmerOrder.U_ID = 1;
        //    consmerOrder.CO_OrderCode = DateTime.Now.ToString() + "1";
        //    consmerOrder.CO_OrderType = 3;
        //    consmerOrder.MC_ID = Convert.ToInt32(MC_ID);
        //    consmerOrder.MC_CardID = id;
        //    consmerOrder.EG_ID =Convert.ToInt32( EG_ID);
        //    consmerOrder.CO_TotalMoney = 0;
        //    consmerOrder.CO_DiscountMoney = 0;
        //    consmerOrder.CO_GavePoint = Convert.ToInt32(CO_GavePoint);
        //    consmerOrder.CO_Recash = 0;
        //    consmerOrder.CO_Remark = "兑换礼物";
        //    consmerOrder.CO_CreateTime = DateTime.Now;
        //    if (Add(consmerOrder))
        //    {
        //        MemCards mem;
        //        if (id.Length < 11)
        //        {
        //            mem = memCard.Find(m => m.MC_CardID == id);
        //        }
        //        else
        //        {
        //            mem = memCard.Find(m => m.MC_Mobile == id);
        //        }
        //        if (mem != null)
        //        {
        //            mem.MC_Point = mem.MC_Point - Convert.ToInt32(CO_GavePoint);

        //            if (memCard.Update(mem))
        //            {
        //                ExchangGifts gift;
        //                gift = Gifts.Find(g => g.EG_ID == EG_ID);
        //                if (gift != null)
        //                {
        //                    gift.EG_Number = gift.EG_Number - 1;
        //                    gift.EG_ExchangNum = gift.EG_ExchangNum + 1;

        //                    if (Gifts.Update(gift))
        //                    {
        //                        return new OperateResult(code.Success, "兑换成功！");
        //                    }
        //                }

                        
        //            }
        //        }
        //    }
        //        return new OperateResult(code.Error, "兑换失败！");
            


        //}
    }
}
